// bindpage.js 手机号码绑定
var shihan = require('../../common/fetch')
var config = require('../../common/config')
var pages, prevPage, upperPage;
var sendflag = true;
var app = getApp();
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindactive: '',
    codeactive: '',
    codeword: '获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载0
   */
  onLoad: function(options) {
    console.log(options)
    console.log(config)
    this.setData({
      orderpayflag: options.orderpayflag || 0
    })
    sendflag = true;
    pages = getCurrentPages();
    if (pages.length >= 2) {
      prevPage = pages[pages.length - 2];
      upperPage = pages[pages.length - 3];
      prevPage.setData({
        goregister: false
      })
    }
  },
  getphone: function(e) {
    // console.log(e)
    this.setData({
      mobile: e.detail.value
    })
  },
  sendcode: function() {
    var that = this;
    if (sendflag) {
      sendflag = false;
      if (!that.data.mobile) {
        shihan.showToast.call(that, '请输入手机号');
        sendflag = true;
        return;
      }
      var open_id = wx.getStorageSync('open_id');
      if (!open_id) {
        wx.setStorageSync('open_id', that.data.mobile); //将手机号作为临时openId
      }
      shihan.sendRequest(config.captcha, 'POST', { mobile: that.data.mobile, type: 'slogin' })
        .then(function(response) {
          if (response.data.code == 0) {
            shihan.showToast.call(that, '验证码发送成功');

            //60秒倒计时
            var time = 60;
            var t = setInterval(function() {
              time--;
              that.setData({
                codeword: time + 's'
              })
              if (time == 0) {
                clearInterval(t);
                that.setData({
                  codeactive: 'codeactive',
                  codeword: '重新发送'
                })
                sendflag = true;
              }
            }, 1000);
          } else {
            sendflag = true;
            shihan.showToast.call(that, response.data.msg);
          }
        }, function(error) {
          console.log(error);
        });
    }
  },
  getcode: function(e) {
    this.setData({
      captcha: e.detail.value
    })
  },
  formSubmit: function(e) {
    var that = this;
    if (!that.data.captcha || !that.data.mobile) {
      shihan.showToast.call(that, '请输入验证码和手机号');
      return;
    }

    that.setData({
      formid: e.detail.formId
    });

    var data = {};
    data["channel_id"] = config.cid;
    data["partner_id"] = config.partner_id;
    data["mobile"] = that.data.mobile;
    data["captcha"] = that.data.captcha;
    data["openid_code"] = that.data.getOpenIdCode; //解析open_id用的code

    shihan.sendRequest(config.wechatLogin, 'POST', data)
      .then(function(response) {

        if (response.data.code == 0) {
          shihan.showToast.call(that, '绑定成功');

          wx.setStorageSync('customerId', response.data.data.sh_customer_id); //用户ID
          wx.setStorageSync('open_id', response.data.data.sh_open_id); //open_id
          wx.setStorageSync('token', response.data.data.sh_token); //用户登录凭证token
          app.submissionForm(that.data.formid, '1', '1');

          that.registerJump();

        } else {
          that.refreshCode(); //绑定失败后需要重新刷新code
          wx.showModal({
            title: '提示',
            content: response.data.msg,
            confirmText: "确认",
            confirmColor: "#c40000",
            showCancel: false
          })
        }
      }, function(error) {
        shihan.showToast.call(that, '额，出错了，请稍后重试');
      });
  },

  registerJump: function() {
    if (config.webresiter || config.specialpage) {
      var token = wx.getStorageSync('token');
      var open_id = wx.getStorageSync('open_id');
      config.specialpage = 0
      if (upperPage.data.weburl&&upperPage.data.weburl.indexOf('?') > 0) {
        upperPage.setData({
          weburl: upperPage.data.weburl + '&token=' + token
        })
      } else {
        upperPage.setData({
          weburl: upperPage.data.weburl + '?token=' + token
        })
      }
    }
    setTimeout(function() {
      wx.navigateBack({
        delta: 2
      })
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    sendflag = true;
    var that = this
    that.setData({
      codeactive: '',
      codeword: '获取验证码'
    })
    that.refreshCode(); //绑定失败后需要重新刷新code
  },
  refreshCode: function() {
    var that = this
    wx.login({
      success: function(res) {
        that.setData({
          'getOpenIdCode': res.code
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})